#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*cmp_fn)(void *v1, void *v2);
int int_cmp(void *v1, void *v2)
{
	return *(int *)v1 - *(int *)v2;
}
int bubble_sort(void *a, int size, int len, cmp_fn cmp)
{
	int i, j;
	void *tmp = malloc(size);
	for (i = len-1; i > 0; i--){
		for (j = 0; j < i; j++) {
			if (cmp((char *)a+j*size, (char *)a+(j+1)*size) > 0){
				memcpy(tmp, (char *)a+j*size, size);
				memcpy((char *)a+j*size, (char *)a+(j+1)*size, size);
				memcpy((char *)a+(j+1)*size, tmp, size);
			}
				
		}
	}
	free(tmp);
}
void main(void)
{
	int i;
	int a[] = {4,1,5,7,3,2,6};
	for (i = 0; i < 7; i++) 
		printf ("%d ", a[i]);
	printf("\n");
	bubble_sort(a, sizeof(int), sizeof(a)/sizeof(int), int_cmp);
	for (i = 0; i < 7; i++) 
		printf ("%d ", a[i]);
	printf("\n");

}
